package beanstao.util.io;

import java.io.IOException;
import java.io.Reader;

public class StringReader extends Reader
{

	private final CharSequence cs;
	private int index;

	public StringReader(CharSequence cs)
	{
		this.cs = cs;
		this.index = 0;
	}

	@Override
	public void close() throws IOException
	{
	}

	@Override
	public int read(char[] cbuf, int off, int len) throws IOException
	{
		if (this.index >= this.cs.length())
		{
			return -1;
		}
		int count = 0;
		for (int i = off; i < (off + len); i++)
		{
			if (this.index >= this.cs.length())
			{
				return count;
			}
			cbuf[i] = this.cs.charAt(this.index++);
			count++;
		}
		return count;
	}

}
